<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/11
 * Time: 17:28
 */
namespace app\admin\collection;
use app\admin\model\Permission;
use think\Db;

class  permissionCollection{
    /**
     * 【根据条件查询user_role表】
     * @param $user_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getUserGroupRoleByUserId($user_id){
        $where = array('user_id'=>$user_id);
        return Db::name("user_role")
            ->where($where)
            ->field('id,group_id,role_id')
            ->find();
    }


    /**
     * 【根据部门及角色查询所拥有的权限】
     * @param $group_id
     * @param $role_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getUserPermissionByGroupRole($group_id, $role_id){
        $where = array('group_id'=>$group_id, 'role_id'=>$role_id,);
        return Db::name("role_permission")
            ->where($where)
            ->field('permission_id')
            ->find();
    }

    /**
     * 【根据节点ID获取节点信息】
     * @param $node_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getNodeById($node_id){
        $where = array('id'=>$node_id);
        return Db::name("permission")
            ->where($where)
            ->field('id,name,path,level,pid,type,status,if_menu')
            ->find();
    }

    /**
     * 【根据用户id查询用户权限】
     * @param $user_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getUserPermissionByUserId($user_id){
        //根据用户id获取部门角色
        $userGroupRole = self::getUserGroupRoleByUserId($user_id);

        //根据部门角色查询权限
        $userPermission = self::getUserPermissionByGroupRole($userGroupRole['group_id'],$userGroupRole['role_id']);

        return $userPermission;
    }

    /**
     * [获取所有节点]
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getAllPermission(){
        $userPermission = Permission::where('status','>=',0)->field('id')->select();
        $id_arr = array_column($userPermission,'id');
        return implode(',', $id_arr);
    }

    /**
     * 【验证用户权限】
     * @param $path
     * @param $user_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function CheckPermission($path, $user_id){
        //根据路径获取节点信息

        $nodes_info = self::getNodeByPath(ucwords($path));
        //获取用户拥有的节点权限集
        $user_nodes_str = session('user_permission');
        if(!in_array($nodes_info['id'],explode(',',$user_nodes_str))){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 【根据路径获取节点信息】
     * @param $path
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getNodeByPath($path){

        $where = array('path'=>$path);
        return Db::name("permission")
            ->where($where)
            ->field('id,name,path,level,pid,type,status,if_menu')
            ->find();
    }

    /**
     * [根据角色ID获取角色信息]
     * @param $role_id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getRoleByRoleId($role_id){
        $where = array('id'=>$role_id);
        return Db::name("role")
            ->where($where)
            ->field('id,name,status')
            ->find();
    }

    /**
     * ['根据权限串，获取权限列表']
     * @param $string
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getPermissionByPermissionString($string){
            $where = array('pid'=>0, 'type'=>0, 'status'=>0);
            $main_menu = Db::name("permission")
                ->where($where)
                ->where('id', 'in' , $string)
                ->field('id,name,path')
                ->order('id asc')
                ->select();
            //获取各主菜单的子菜单
            if(count($main_menu) > 0){
                foreach($main_menu as $key=>&$v){
                    //查询是否有子菜单
                    $has_child = Db::name("permission")
                        ->where('status','=',0)
                        ->where('pid', $v['id'])
                        ->field('id,name,path')
                        ->order('id asc')
                        ->select();
                    if(count($has_child) > 0){
                        $v['child'] = $has_child;
                    }
                }
            }
            return $main_menu;
    }

    /**
     * ['根据部门ID查询所有部门会员']
     * @param $string
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function getUsersByGroupId($group_id=0){
        //获取当前部门所有用户
        $users = Db::table('tp_user_role')
            ->where('ur.group_id','=',$group_id)
            ->field('u.id,u.username,u.nickname')
            ->alias('ur')
            ->join('tp_users u','ur.user_id = u.id','LEFT')
            ->select();
        return $users;
    }



}